var Q = require("q");
var entityCollection = require('../../../Base/server/controllers/EntityCollection');

//核准
exports.syncSaveItem = function(req, res) {
	find(req, res).then(insertOrUpdate).catch(function(err) {
		res.json({
			Error: err
		})
	}).done();
};

//开立
exports.syncDeleteItem = function(req, res) {
	find(req, res).then(deleteItem).catch(function(err) {
		res.json({
			Error: err
		})
	}).done();
};

function find(req, res) {
	var deferred = Q.defer();
	var entityItem = entityCollection.getEntity("ItemMaster");
	//var parameter = req.body;
	var parameter = req.body; //.Parameter,
	var ID = parameter.ID;
	var code = parameter.Code;
	entityItem.find({
		ErpId: ID
	}, null, null, function(err, records) {
		if (!err) {
			var params = {
				req: req,
				res: res,
				records: records
			};
			deferred.resolve(params);
		} else {
			deferred.reject(err);
		}
	});

	return deferred.promise;
};

function insertOrUpdate(result) {
	var deferred = Q.defer();
	var entityItem = entityCollection.getEntity("ItemMaster");
	var entityExtend = entityCollection.getEntity("ExtendEnum");
	var parameter = result.req.body; //.Parameter;
	//var parameter = result.req.body.Parameter;
	var ID = parameter.ID;
	var code = parameter.Code;
	var record = {};
	if (result.records.length < 1) {
		record.Code = parameter.Code;
		record.Name = parameter.Name;
		record.Specification = parameter.SPECS;
		record.IsEffective = parameter.Effective_IsEffective;
		record.ErpId = ID;
		record.Remark = "";
		//record.ItemCategory=;
		if (parameter.MainItemCategoryName != undefined && parameter.MainItemCategoryName.trim() != null) {
			entityExtend.find({
				Code: 'ItemCategory'
			}, null, null, function(err, ops) {
				if (ops.length > 0 && ops[0].Options.length > 0) {
					for (var i = 0; i < ops[0].Options.length; i++) {
						if (ops[0].Options[i].Name == parameter.MainItemCategoryName) {
							record.ItemCategory = ops[0].Options[i].Value;
						};
					};
				};
				record.ItemCategory = 0;
				entityItem.insert(record, function(err) {
					if (err) {
						deferred.reject(err);
					} else {
						deferred.resolve(record);
						result.res.json({
							Error: ''
						});
					}
				});
			});
		} else {
			entityItem.insert(record, function(err) {
				if (err) {
					deferred.reject(err);
				} else {
					deferred.resolve(record);
					result.res.json({
						Error: ''
					});
				}
			});
		};

	} else if (result.records.length >= 1) {
		record.Specification = parameter.SPECS;
		//record.ItemCategory=;
		record.IsEffective = parameter.Effective_IsEffective;
		//record.ErpId = ID;
		entityItem.updateByValue({
			ErpId: ID
		}, record, function(err, rcd) {
			if (err) {
				deferred.reject(err);
			} else {
				deferred.resolve(rcd);
				result.res.json({
					Error: ''
				});
			}
		});
	};

	return deferred.promise;
};

function deleteItem(result) {
	var deferred = Q.defer();
	var entityItem = entityCollection.getEntity("ItemMaster");
	var entityMO = entityCollection.getEntity("MO");
	var parameter = result.req.body; //.Parameter;
	var ID = parameter.ID;
	var record = {};
	entityMO.find({
		ItemMaster: result.records[0]._id
	}, null, null, function(err, rcds) {
		if (err) {
			deferred.reject(err);
		} else {
			if (rcds.length > 0) {
				//
				deferred.reject({
					Error: '物料在数采系统中已有工单'
				});
			} else {
				//删除Item
				entityItem.deleteByCondition({
					ErpId: ID
				}, function(ierr) {
					if (ierr) {
						deferred.reject(ierr);
					} else {
						deferred.resolve();
						result.res.json({
							Error: ''
						});
					}
				});
			};
		}
	});
	return deferred.promise;
};